package com.zzhrtech.wgzx_cityzen.ui.disease;


import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.GsonRequest;
import com.github.clans.fab.FloatingActionButton;
import com.github.clans.fab.FloatingActionMenu;
import com.sunfusheng.marqueeview.MarqueeView;
import com.zzhrtech.WgzxApplication;
import com.zzhrtech.wgzx_cityzen.Constans;
import com.zzhrtech.wgzx_cityzen.R;
import com.zzhrtech.wgzx_cityzen.adapters.disease.DiseaseTypeAdapter;
import com.zzhrtech.wgzx_cityzen.base.BaseFragment;
import com.zzhrtech.wgzx_cityzen.base.OnItemClickLitener;
import com.zzhrtech.wgzx_cityzen.beans.disease.CateAllBean;
import com.zzhrtech.wgzx_cityzen.beans.me.ShuomingBean;
import com.zzhrtech.wgzx_cityzen.utils.DensityUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link DiseaseFragmentCopy#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DiseaseFragmentCopy extends BaseFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters

    //    private RecyclerView rv_disease;
    private DiseaseTypeAdapter typeAdapter;

    private List<CateAllBean.Data> cateBean;

    private LinearLayout iconLayout;
    private MarqueeView marqueeView2;
    List<String> piaoList = new ArrayList<>();

    public DiseaseFragmentCopy() {
        // Required empty public constructor
    }


    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment DiseaseFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static DiseaseFragmentCopy newInstance(String param1, String param2) {
        DiseaseFragmentCopy fragment = new DiseaseFragmentCopy();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_disease, container, false);

        iconLayout = (LinearLayout) view.findViewById(R.id.iconLayout);

        marqueeView2 = (MarqueeView) view.findViewById(R.id.marqueeView2);



        marqueeView2.setOnItemClickListener(new MarqueeView.OnItemClickListener() {
            @Override
            public void onItemClick(int position, TextView textView) {
                Intent intent = new Intent(getActivity(),GonggaoActivity.class);
                startActivity(intent);
            }
        });
        return view;
    }

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {

            if (msg.what == 0) {

                cateBean = (List<CateAllBean.Data>) msg.obj;

                for (int i = 0; i < cateBean.size(); i++) {



                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);


                    TextView textView = new TextView(mActivity);


                    layoutParams.weight = 1;

//                    if (cateBean.size() - 1 == i){
//                        layoutParams.leftMargin = 60;
//                        layoutParams.rightMargin = 60;
//                    }else {
//                        layoutParams.leftMargin = 60;
//                    }

                    textView.setLayoutParams(layoutParams);


                    Drawable drawable = null;
                    if (cateBean.get(i).getCat_name().contains("排水")) {
                        drawable = getResources().getDrawable(R.mipmap.ic_dis_paishui);
                    } else if (cateBean.get(i).getCat_name().contains("道路")) {
                        drawable = getResources().getDrawable(R.mipmap.ic_dis_daolu);
                    } else if (cateBean.get(i).getCat_name().contains("桥梁")) {
                        drawable = getResources().getDrawable(R.mipmap.ic_dis_qiaol);
                    } else if (cateBean.get(i).getCat_name().contains("停车")) {
                        drawable = getResources().getDrawable(R.mipmap.ic_dis_tingche);
                    } else if (cateBean.get(i).getCat_name().contains("防汛")) {
                        drawable = getResources().getDrawable(R.mipmap.ic_dis_fangxun);
                    } else {
                        drawable = getResources().getDrawable(R.mipmap.ic_dis_paishui);
                    }
                    /// 这一步必须要做,否则不会显示.
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    textView.setCompoundDrawables(null, null, null, drawable);
                    textView.setCompoundDrawablePadding(20);

                    textView.setGravity(Gravity.CENTER);

                    textView.setTextColor(getResources().getColor(R.color.white));
                    textView.setTextSize(18);
                    textView.setText(cateBean.get(i).getCat_name());

                    final int finalI = i;
                    textView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(getActivity(), DiseaseUploadActivity.class);
                            intent.putExtra("depid", cateBean.get(finalI).getDep_id());
                            intent.putExtra("catid", cateBean.get(finalI).getCat_id());
                            intent.putExtra("catname", cateBean.get(finalI).getCat_name());
                            mActivity.startActivityForResult(intent, 2);
                        }
                    });
                    iconLayout.addView(textView);
                }
            }else if (msg.what == 1){
                ShuomingBean bean = (ShuomingBean) msg.obj;

                List<ShuomingBean.Data> dataList; dataList = bean.getData();

                for (ShuomingBean.Data data : dataList){
                    piaoList.add(data.getTitle());
                }
                marqueeView2.startWithList(piaoList);
            }
//            for (CateAllBean.Data data : cateBean){
//                typeAdapter.addItem(data);
//            }

            return false;
        }
    });

    @Override
    protected void initData() {
        super.initData();

        GsonRequest<CateAllBean> gsonRequest = new GsonRequest<CateAllBean>(com.android.volley.Request.Method.GET, Constans.HTTP_CATELISTALL, CateAllBean.class, null, new com.android.volley.Response.Listener<CateAllBean>() {
            @Override
            public void onResponse(CateAllBean response) {

                if (response.isSuccess()) {
                    Message message = new Message();
                    message.what = 0;
                    message.obj = response.getData();
                    mHandler.sendMessage(message);
                } else {
//                    showToast(tv_time,response.getMsg());
                }

            }
        }, new com.android.volley.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("result=====", error + "");
//                dialog.dismiss();
            }
        });

        WgzxApplication.addRequest(gsonRequest, Constans.reqTag);


        httpGetArea();

    }



    //    private Handler mHandler = new Handler(new Handler.Callback() {
//        @Override
//        public boolean handleMessage(Message msg) {
//
//            ShuomingBean bean = (ShuomingBean) msg.obj;
//
//            dataList = bean.getData();
//            baseUrl = bean.getLink();
//            for (ShuomingBean.Data data : dataList){
//                shuomingAdapter.addItem(data);
//            }
//
//            return false;
//        }
//    });
//
    private void httpGetArea(){

        GsonRequest<ShuomingBean> gsonRequest = new GsonRequest<ShuomingBean>(com.android.volley.Request.Method.POST, Constans.HTTP_CITIPO, ShuomingBean.class, null, new com.android.volley.Response.Listener<ShuomingBean>() {
            @Override
            public void onResponse(ShuomingBean response) {

                if (response.isSuccess()){
                    Message message = new Message();
                    message.what = 1;
                    message.obj = response;
                    mHandler.sendMessage(message);
                }else {
                    showToast(response.getMsg());
                }
            }
        }, new com.android.volley.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
//                AppLogger.d(error + "");
            }
        });

        WgzxApplication.addRequest(gsonRequest, Constans.reqTag);
    }


}
